Skip to main content

Virtual Class in C++

 

#include<bits/stdc++.h>
using namespace std;
class A{
    protected:
    int roll;
    public:
    void set_roll(int r){
        roll=r;
    }
    void print_roll(){
       
        cout<<"The Roll No. is: "<<roll<<endl;
    }

};
class B: virtual public A{
    protected:
    double math;
    double english;
    public:
    void set_marks(double m, double e){
        math=m;
        english=e;
    }
    void print_marks(){
       
        cout<<"The Marks in Math: "<<math<<endl;
        cout<<"The Marks in English: "<<english<<endl;
    }

};
class C: virtual public A{
    protected:
    double score;
    public:
    void set_score(double s){
        score=s;
    }
    void print_score(){
       
        cout<<"Your score is: "<<score<<endl;
    }

};
class D: public B, public C{
    public:
    void display(){
        print_roll();
        print_marks();
        print_score();
        cout<<"Total Marks obtaiend is: "<<math+english+score<<endl;
        cout<<"Your Got total: "<<(math+english+score)/3<<"%"<<endl;
    }
};
int main()
{
    D i;
    i.set_roll(4025);
    i.set_marks(75,86.0);
    i.set_score(69);
    i.display();
    return 0;
}




Comments